package mylibrary.activeMQ;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class JMSSelectorTest {

	public static void main(String[] args) throws JMSException {
		
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		Connection connection = factory.createConnection();
		connection.start();

		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		Queue queue = new ActiveMQQueue("testQueue");  
		
		MessageConsumer consumerA = session.createConsumer(queue, "receiver = 'A'");
		
		consumerA.setMessageListener(new MessageListener() {  
            public void onMessage(Message m) {  
                try {  
                    System.out.println("ConsumerA get " + ((TextMessage) m).getText());  
                } catch (JMSException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
		
		MessageConsumer consumerB = session.createConsumer(queue, "receiver = 'B'");
		
		consumerB.setMessageListener(new MessageListener() {  
            public void onMessage(Message m) {  
                try {  
                    System.out.println("ConsumerB get " + ((TextMessage) m).getText());  
                } catch (JMSException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
		
		MessageProducer producer = session.createProducer(queue);
		
		for(int i = 0; i < 10; i++) {
			String receiver = (i%2==0) ? "A" : "B";
			Message msg = session.createTextMessage("Message " + i);
			msg.setStringProperty("receiver", receiver);
			
			producer.send(msg);
		}
	}
}
